package com.wireMockEurekaServer.WireMockEurekaServer;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.common.SingleRootFileSource;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;

@SpringBootApplication
public class WireMockEurekaServerApplication {
	
	@Value("${wireMock.address}")
	private String wireMockAddress;
	
	@Value("${wireMock.port}")
	private String wireMockPort;
	
	@Value("${wireMock.filesRoot}")
	private String wireMockFilesRoot;
	
	
	@Value("${spring.application.name}")
	private String appName;
	
	@Bean
	public RestTemplate restTemplate(RestTemplateBuilder builder) {
		return builder.build();
	}
	
	@Bean
	public WireMockServer wireMockServer(){
		//创建wiremock对象
		FileSource filesRoot = new SingleRootFileSource(wireMockFilesRoot+"/"+appName);
		WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.options()
				.bindAddress(wireMockAddress).port(Integer.parseInt(wireMockPort)).fileSource(filesRoot));
		wireMockServer.start();
		return wireMockServer;
	}
	
	public static void main(String[] args) {
		SpringApplication.run(WireMockEurekaServerApplication.class, args);
	}
}
